<template>
  <div class="app-container">

    <!-- 这里是引入组件部分 -->
    <topIndex style="margin-bottom: 20px;" :indexCount="indexCount"></topIndex>
    <el-form :model="queryParams" class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch"
      label-width="68px">
      <!-- 这里是搜索栏部分 -->
      <div class="from-item">
        <el-form-item label="微信名称" prop="custWcId">
          <el-input v-model="queryParams.custWcId" @keyup.enter.native="handleQuery" placeholder="请输入微信用户名称" clearable />
        </el-form-item>

        <el-form-item label="所属助手" v-if="!common" prop="assistId">
          <el-input v-model="queryParams.assistId" @keyup.enter.native="handleQuery" placeholder="请输入助手名称" clearable />
        </el-form-item>

        <el-form-item label="提醒时间" prop="remindTimeBegin">
          <el-date-picker v-model="chatTimes" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>



        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="queryParams.keywords" @keyup.enter.native="handleQuery" placeholder="请输入关键词" clearable />
        </el-form-item>

        <el-form-item label="处理时间" prop="handleTimeBegin">
          <el-date-picker v-model="chatTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
      </div>
      <div class="from-Btn">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>
    </el-form>

    <div class="line-state">
        <div class="state-box" @click="setState(item.value)" :class="state==item.value?index==0?'select frist-border':index==2?'select end-border':'select':index==0?'frist-border':index==2?'end-border':''" :style="index==1?'border-right: 1px solid #d1e5ff;border-left: 1px solid #d1e5ff;':''" v-for="item,index in finishState" :key="index">{{ item.label }}</div>
    </div>
    <!-- 这里是信息栏部分 -->
    <el-table v-loading="loading" :data="wcChatRemindList" :header-cell-style="{ background: '#fcfcfd', color: '#000' }"
      :border="false">
      <el-table-column label="微信名称" prop="custWcId" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="聊天形式" prop="chatShape" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.chatShape==0?'好友':'群聊'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属助手" prop="assistId" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="提醒时间" prop="remindTime" align="center" :show-overflow-tooltip="true" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remindTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="关键词" prop="keywords" align="center" :show-overflow-tooltip="true" />
      <el-table-column v-if="state" label="处理时间" prop="handleTime" align="center" :show-overflow-tooltip="true" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="state" label="状态" prop="handleState" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span :style="scope.row.handleState==1?'color:#97c05a':'color:#c8544b'">{{ scope.row.handleState==1?'任务已完成':scope.row.handleState==2?'任务已取消':'任务待处理'}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" v-if="checkPermi(['massage:massage:getMags'])||!state">
        <template slot="header">
          <span>操作</span>
          <el-tooltip class="item" effect="dark" content="进入操作" placement="top">
            <i class="el-icon-warning" style="color:#8c8c8c;"></i>
          </el-tooltip>
        </template>

        <template slot-scope="scope">
          <el-button type="warning" v-if="!state" style="background-color: #fb9d52;color: #fff;width: 50px;" icon="el-icon-circle-check" @click="setHandle(scope.row.id,1)" size="mini">确认</el-button>
          <el-button type="warning" style="background-color: #fb9d52;color: #fff;width: 60px;" icon="el-icon-document" size="mini" v-hasPermi="['massage:massage:getMags']" @click="golink(scope.row.wcId,scope.row.chatUserId,scope.row.chatGroupId,scope.row.custWcId)">{{state==0?'去处理':'详情'}}</el-button>
          <el-button type="info" v-if="!state" style="background-color: #d7caca;border-color: #d7caca;color: #fff;width: 50px;" icon="el-icon-circle-close" @click="setHandle(scope.row.id,2)" size="mini">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />


  </div>
</template>
  
<script>
import moment from "moment";
import request from '@/utils/request'
import mathNum from "../components/daily.js"
import nabars from '../components/nabar.vue'
import topIndex from '../components/top-index.vue'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
export default {
  name: "WcUserInfo",
  dicts: ['sys_cycle_type'],
  components: {
    nabars,
    topIndex,
  },
  data() {
    return {
      activeName: 'first',//表示默认就是在第一个上面
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      wcChatRemindList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wcId:null,
        deptId:null,
        wcName: null,
        keywords:null,
        custWcId:null,
        assistId:null,
        handleState:0,
        remindTimeBegin:null,
        handleTimeBegin:null,
        remindTimeEnd:null,
        handleTimeEnd:null,
      },
      state:0,
      common:false,
      chatTime:[],
      chatTimes:[],
      finishState:[{label:'任务待执行',value:0},{label:'任务执行完成',value:2}],
      indexCount: [{ name: '今日提醒', num: 0, type: 0, bili: 0 }, { name: '今日已处理', num: 0, type: 0, bili: 0 }, { name: '今日未处理', num: 0, type: 0, bili: 0 }],
    };
  },
  created() {
    //console.log("wxid===", this.$store.state.user.wxid)
  },
  mounted() {
    if(this.$store.state.user.roles.includes("common")){
       this.common=true
    }
    this.getList();
    this.init()
  },
  methods: {
    checkPermi,
    init(){
      // var wxId=this.$store.state.user.wxid
      // var userId=this.$store.state.user.userid
      // var deptId=this.$store.state.user.deptid
      // var data={userId,wxId,deptId}
      getnewRemind().then(res=>{
        if(res.code==200){
            var num=0
            this.indexCount[0].type=mathNum(res.data.new_size,res.data.old_size).split('_')[0]
            this.indexCount[0].bili=mathNum(res.data.new_size,res.data.old_size).split('_')[1]
            this.indexCount[0].num=res.data.new_size
        }
      })
      getRemids().then(res=>{
        if(res.code==200){
            this.indexCount[1].type=mathNum(res.data.new_size,res.data.old_size).split('_')[0]
            this.indexCount[1].bili=mathNum(res.data.new_size,res.data.old_size).split('_')[1]
            this.indexCount[1].num=res.data.new_size
          }
      })
      getNotRemids().then(res=>{
        if(res.code==200){
            this.indexCount[2].type=mathNum(res.data.new_size,res.data.old_size).split('_')[0]
            this.indexCount[2].bili=mathNum(res.data.new_size,res.data.old_size).split('_')[1]
            this.indexCount[2].num=res.data.new_size
          }
      })
    },
    setState(id){
        this.state=id
        if(id==0){
          this.queryParams.handleState = id
        }else{
          this.queryParams.handleState=-1
        }
        this.getList();
    },
    setHandle(id,type){
      var handleTime=moment().format('YYYY-MM-DD HH:mm:ss');
      this.$modal.confirm('是否确认修改').then(function() {
        return upadetRemind({id,handleState:type,handleTime});
      }).then(() => {
        this.$emit("changehandle",id)
        this.getList();
        this.$modal.msgSuccess("修改成功");
      }).catch(() => {});
    },
    golink(wxid,userid,groupid,username) {
      //console.log(wxid,userid,groupid)
      this.$router.push({ path:"/market/detial/index/",name:'frienddetial',query: { wxid,userid,groupid,name:'',username}}).catch(()=>{});
    },
    /** 查询用户微信列表 */
    getList() {
      this.loading = true;
      listWcChatRemind(this.queryParams).then(res => {
        if (res['success']) {
          const dt = res['data'];
          this.total = dt['total'];
          this.wcChatRemindList = dt['records'];
          this.$emit("changehandle",this.wcChatRemindList.length+'length')
        }
        this.loading = false;
      });
      setTimeout(()=>{
        this.loading = false;
      },2000)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.remindTimeBegin=null
      this.queryParams.remindTimeEnd=null
      if(this.chatTimes!=''&&this.chatTimes!=null){
        this.queryParams.remindTimeBegin=this.chatTimes[0]+' 00:00:00'
        this.queryParams.remindTimeEnd=this.chatTimes[1]+' 23:59:59'
      }
      this.queryParams.handleTimeBegin=null
      this.queryParams.handleTimeEnd=null
      if(this.chatTime!=''&&this.chatTime!=null){
        this.queryParams.handleTimeBegin=this.chatTime[0]+' 00:00:00'
        this.queryParams.handleTimeEnd=this.chatTime[1]+' 23:59:59'
      }
      //console.log(this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.chatTime=[]
      this.chatTimes=[]
      if(this.state==0){
        this.queryParams.handleState = this.state
      }else{
        this.queryParams.handleState=-1
      }
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWcUserInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户微信";
      });
    },

  }
};

/** js请求 调用 */
//查询提醒记录
export function listWcChatRemind(query) {
  return request({
    url: '/wxassistant/wcchat_remind/page',
    method: 'get',
    params: query
  })
}
//修改提醒
export function upadetRemind(query) {
  return request({
    url: '/wxassistant/wcchat_remind/edit',
    method: 'POST',
    data: query
  })
}
//查询变化
export function getnewRemind(query) {
  return request({
    url: '/wxassistant/wcchat_remind/newWcChatRemind',
    method: 'get',
    data: query
  })
}

export function getRemids(query) {
  return request({
    url: '/wxassistant/wcchat_remind/ProcessedWcChatRemind',
    method: 'get',
    data: query
  })
}
export function getNotRemids(query) {
  return request({
    url: '/wxassistant/wcchat_remind/UntreatedWcChatRemind',
    method: 'get',
    data: query
  })
}
</script>
<style scoped lang="scss">
.app-container {
  background-color: #f6f9fb;
  display: flex;
  flex-direction: column; // 确保子元素垂直排列

  .el-tabs {
    flex: 1; // 允许 tabs 占据剩余空间
    display: flex;
    flex-direction: column; // 确保标签页垂直排列
  }
}

.frominfo {
  display: flex;
  background-color: #fff;
  padding: 10px;
  padding-bottom: 0;
  margin-bottom: 20px;

  .from-item {
    display: flex;
    flex-wrap: wrap;
  }

  .from-Btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 20%;
  }
}
.line-state{
      display: flex;
      width: 260px;
      border:1px solid #d1e5ff;
      font-size: 14px;
      border-radius: 5px;
      background-color: #fff;
      .state-box{
        width: 130px;
        height: 35px;
        color: #8c8c8c;
        text-align: center;
        line-height: 35px;
      }
      .frist-border{
        border-top-left-radius: 5px;
        border-bottom-left-radius: 5px;
      }
      .end-border{
        border-top-right-radius: 5px;
        border-bottom-right-radius: 5px;
      }
      .select{
        color: #fff;
        background-color: #288dfc;
      }
    }

</style>